<?php namespace App\Http\Controllers;

use App\Banner;
use App\Goods;
use App\GoodsImg;
use App\Orders;
use App\Recommend;
use App\User;
use App\UserAddress;
use Illuminate\Auth\Guard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;

class HomeController extends Controller {

	/*
	|--------------------------------------------------------------------------
	| Home Controller
	|--------------------------------------------------------------------------
	|
	| This controller renders your application's "dashboard" for users that
	| are authenticated. Of course, you are free to change or remove the
	| controller as you wish. It is just here to get your app started!
	|
	*/

	/**
	 * Create a new controller instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
//		$this->middleware('auth');
	}

	/**
	 * Show the application dashboard to the user.
	 *
	 * @return Response
	 */
	public function index()
	{
	    //banner
	    $banners = Banner::whereStatus(1)->get();

	    //推荐栏目
        $recommendArry['hot'] = Recommend::whereType('hot')->whereStatus(1)->first();
        $recommendArry['new'] = Recommend::whereType('new')->whereStatus(1)->first();

        $goods = Goods::whereIsshop(1)->orderBy('id','desc')->limit(20)->get();
        $goodHots = Goods::whereIsshop(1)->whereIsrecommend(1)->orderBy('id','desc')->limit(10)->get();

		return view('home',[
		    'banners'=>$banners,
            'recommendArry'=>$recommendArry,
            'good_hots'=>$goodHots,
            'goods'=>$goods
        ]);
	}

	public function contentDetail(){
		$id = Input::get('id');
		$thatArticle = Article::find($id);
		return view('content_detail',['article'=>$thatArticle]);
	}

    public function goods()
    {
        $allGoods = Goods::where('isshop','<>',0)->get();
        return view('goods',['goods'=>$allGoods,'page'=>'goods']);
    }

	public function goodsDetail(Request $request)
	{
		$id = $request->get('id');
		/** @var Goods $thatGood */
		$thatGood = Goods::whereId($id)->where('isshop','<>',0)->first();

		if($thatGood){
		    $goodImgs = GoodsImg::whereGoodId($thatGood->id)->get();
		    $thatGood->imgs = $goodImgs;
        }

		return view('goods-detail',['that_good'=>$thatGood]);
	}

	public function confirm(Guard $guard){
		$orderItemId = Input::get('id');
		$addressId   = !is_null(Input::get('addid'))?Input::get('addid'):0;
		$thatOrderItem = OrderItem::find($orderItemId);
		if($guard->guest()){
			$thatOrderItem->defaultaddress = false;
		}else{
			/** @var User $user */
			$user = $guard->user();
            $defaultAddress = null;
            if($addressId == 0){
                $defaultAddress = $user->userAddress()->where('is_default','=',1)->orderBy('created_at','desc')->first();
            }else{
                $defaultAddress = UserAddress::find($addressId);
            }
			if(!$defaultAddress){
				$thatOrderItem->defaultAddress = false;
			}else{
				$thatOrderItem->defaultaddress = $defaultAddress;
			}
		}

		return view('confirm',['thatOrderItem'=>$thatOrderItem]);
	}

	public function choiceAddress(Guard $guard){
		if($guard->guest())
			return redirect(route('M.Home.addAddress'));

		/** @var User $user */
		$user = $guard->user();

		$thatAddress = $user->userAddress()->orderBy('created_at','desc')->get();

		$order_item_id = Session::get('orderitemid');

		return view('choice-address',['alladdress'=>$thatAddress,'order_item_id'=>$order_item_id]);
	}

	public function addAddress()
	{
		$reback = Input::get('reback');
		return view('add-address',['reback'=>$reback]);
	}

	public function payInfo(Guard $guard){
		$orderid = Input::get('id');

		/** @var Orders $thatOrder */
		$thatOrder = Orders::find($orderid);
		if(!$thatOrder)
			throw new NotFoundException(NotFoundException::OrderNotFound);

		$thatOrder->money = (float)$thatOrder->s_amount + (float)$thatOrder->express_fee;
		return view('pay-info',['order'=>$thatOrder]);
	}

	public function orderList(Guard $guard){

		$modelid = !is_null(Input::get('model')) ? Input::get('model') : 0;

		/** @var User $user */
		$user = $guard->user();

		$orders = null;
		switch((int)$modelid){
			case 0:
				$orders = Orders::whereUserId($user->id)->orderBy('created_at','desc')->get();
				break;
			case 1:
				$orders = Orders::whereUserId($user->id)->whereStatus(0)->orderBy('created_at','desc')->get();
				break;
			case 2:
				$orders = Orders::whereUserId($user->id)->whereStatus(2)->orderBy('created_at','desc')->get();
				break;
			case 3:
				$orders = Orders::whereUserId($user->id)->whereStatus(3)->orderBy('created_at','desc')->get();
				break;
		}

		return view('order-list',['orders'=>$orders,'model'=>$modelid,'page'=>'usercenter']);
	}

	public function userCenter(Guard $guard){
		if($guard->guest())
			return redirect(route('M.Home.login'));

		/** @var User $user */
		$user = $guard->user();
		return view('user-center',['user'=>$user,'page'=>'usercenter']);
	}

	public function kefu(){
		return view('kefu',['page'=>'kefu']);
	}

	public function login(){
		return view('login');
	}

	public function signUp(){
		return view('sign-up');
	}

	/** 登出 */
	public function signOut(Guard $guard)
	{
		$guard->logout();
		return redirect(route('M.Home.goods'));
	}

	public function userAddress(Guard $guard){
		if($guard->guest())
			return redirect(route('M.Home.login'));

		/** @var User $user */
		$user = $guard->user();

		$thatAddress = $user->userAddress()->orderBy('created_at','desc')->get();

		return view('user-address',['alladdress'=>$thatAddress,'page'=>'usercenter']);
	}

	public function payTest(){
	    return view('pay-test');
    }

}
